#ifndef GRAPHICS_MANAGER_H
#define GRAPHICS_MANAGER_H
#pragma once

#include <Windows.h>
#include <d3dx9.h>

#include "Utils\SingletonPattern.h"
#include "Utils\Defines.h"
#include "Math\Color.h"
#include "Math\Matrix44.h"
#include "Frustum.h"

class CCamera;

class CGraphicsManager : public CSingleton<CGraphicsManager>
{
public:
	typedef enum{ CENTER, UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, LOWER_RIGHT } ETypeAlignment;

	CGraphicsManager();
	~CGraphicsManager();

	bool Init (HWND hWnd, bool fullscreenMode, uint32 widthScreen, uint32 heightScreen);
	void Update(float32 ElapsedTime);
	void Render();
	void Release();
	void SetupMatrices( CCamera* ap_Camera );
	void BeginRender();
	void EndRender();
	void SetTransform (D3DXMATRIX& matrix);
	void SetTransform (Mat44f& matrix);

	// Getters and setters
	const LPDIRECT3DDEVICE9 GetDevice() const
	{ 
		return m_pD3DDevice;
	}

	// GetSet
	GET_SET(uint32, uWidth);
	GET_SET(uint32, uHeight);

	// Basic Primitives
	void DrawAxis(float32 Size);
	void DrawGrid(float32 Size, CColor Color=colWHITE, int GridX=10, int32 GridZ=10 );
	void DrawPlane(	float32 Size, const Vect3f& normal, float32 distance,CColor Color=colWHITE, int GridX=10, int32 GridZ=10 );
	void DrawCube(float32 Size, CColor Color=colWHITE);
	void DrawBox(float32 SizeX, float32 SizeY, float32 SizeZ, CColor Color=colWHITE);
    void DrawCircle(float32 Radius, CColor Color=colWHITE, int32 Aristas=10);
	void DrawSphere(float32 Radius, CColor Color=colWHITE, int32 Aristas=10);
	void DrawLine(const Vect3f &PosA, const Vect3f &PosB, CColor Color=colWHITE);
	void DrawCamera (CCamera* camera);
	void	 DrawQuad2D (const Vect2i& pos, uint32 w, uint32 h, ETypeAlignment alignment, CColor color = colBLUE);

private:
	HWND				m_WindowId;						// 3D render window handle
	LPDIRECT3D9			m_pD3D;							// direct3d interface
	LPDIRECT3DDEVICE9   m_pD3DDevice;					// direct3d device
	uint32				m_uWidth;						// width of the client windows
	uint32				m_uHeight;						// height of the client windows
	CColor				m_BackbufferColor_debug;		// Clear the backbuffer with this color in debug mode 
	CColor				m_BackbufferColor_release;		// Clear the backbuffer with this color in release mode
	LPDIRECT3DSURFACE9	m_pBackBuffer;
	CFrustum			m_Frustum;
	bool				m_bPaintSolid;
	bool				m_bIsOk;						// Initialization boolean control
	void GetWindowRect( HWND hwnd );

	void CGraphicsManager::CalculateAlignment (uint32 w, uint32 h, ETypeAlignment alignment, Vect2i & finalPos);
};

#endif // GRAPHICS_MANAGER_H
